﻿CREATE PROCEDURE [dbo].[proc_Project_AnalyseFinish]
(
	@OrderId uniqueidentifier
)
AS
BEGIN TRANSACTION--开始事务
Declare @errorSum int	--定义错误计数器
Set @errorSum = 0	--初始化

	Update [Order] Set StateId=3 Where OId=@OrderId
	if @@ROWCOUNT<1
		begin
			set @errorSum=@errorSum+1		
			goto e;
		end

--By 赵洪涛 2012-08-13 将16:30后的数据调整到第2天 Begin--
	Declare @nDays int,@nHour int,@nMinute int,@dToday DateTime
--	Set @dToday = GetDate() --初始化日期
--	--16:30分调整日期
--	Set @nHour = 16
--	Set @nMinute = 30
--
--	--今天+1天与下月1日进行比较，相同则说明今天是本月最后1天
--	If Month(GetDate())<12
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())) + '-' + Convert(varchar(2),Month(GetDate())+1) + '-1'))
--	Else
--		Set @nDays=DateDiff(d,DateAdd(d,1,GetDate()),Convert(datetime,Convert(varchar(4),Year(GetDate())+1) + '-01-01'))
--	If @nDays>0 --不是最后1天
--	Begin
--		If DatePart(hh,GetDate())>@nHour Or (DatePart(hh,GetDate())=@nHour And DatePart(mi,GetDate())>@nMinute)	--超过16:30分
--			Set @dToday = DateAdd(hh,12,GetDate())	--加12小时
--	End
	Set @dToday=dbo.Get_SetDateTime()
--By 赵洪涛 2012-08-13 将16:30后的数据调整到第2天 End --
	Update [Project] Set CreateDate=@dToday Where OrderId=@OrderId



	--------------------以下部分2016.5.20添加-----------------李慧婷---------------------------------------

	---------------------分析的项目中有“网站维护费”的，自动下发给接口人并自动完结----------------
	Declare @Ct int
	SELECT   @Ct=COUNT(*) FROM   Project  WHERE   OrderId = @OrderId and ProductId=69 and ProductName='网站维护费' and IsHedge=0 and  FinanceAudit=1
	
	IF @Ct > 0  --存在网站维护费项目
	  
	   Declare  @ProjectId uniqueidentifier,
			    @CusName nvarchar(200),
	            @ProductId int,
				@ProductName nvarchar(50),
				@ExecDepartmentId int,
				@ExecPerson nvarchar(50),
				@CompanyId int
     Begin
		   Declare My_Cursor CURSOR --定义游标
		   FOR  select ProjectId,CusName,ProductId,ProductName,ExecDepartmentId,ExecPerson,CompanyId from Project where OrderId=@OrderId  and ProductId=69 and ProductName='网站维护费' and IsHedge=0 and FinanceAudit=1
		   OPEN My_Cursor; --打开游标
		   FETCH NEXT FROM My_Cursor INTO @ProjectId,@CusName,@ProductId,@ProductName,@ExecDepartmentId,@ExecPerson,@CompanyId; --读取第一行数据

		   WHILE (@@FETCH_STATUS = 0)
			BEGIN
					 Declare @ExecDepartmentName nvarchar(50),@DownPersonName nvarchar(50)
			
					 Select @ExecDepartmentName=DName From Department where Id=@ExecDepartmentId --任务执行人部门名称
					 Select @DownPersonName=EName From Employee Where UserName=@ExecPerson --任务下发人姓名

					 Insert Into Project_Task --增加一条任务，状态为完成
						(
							FId
							,OrderId
							,ProjectId
							,ProjectName
							,ProductId
							,CusName
							,Title
							,WebType
							,Description
							,DownPerson
							,DownPersonName
							,ExecDepartmentId
							,ExecDepartmentName
							,ExecPerson
							,ExecPersonName
							,StartDate
							,EndDate
							,IsFinish
							,CreateDate
							,CompanyId
							,TypeId
						)
						Values
						(
							@ProjectId
							,@OrderId
							,@ProjectId
							,@ProductName
							,@ProductId
							,@CusName
							,'网站维护费'
							,0
							,'网站维护费--自动完结'
							,@ExecPerson
							,@DownPersonName
							,@ExecDepartmentId
							,@ExecDepartmentName
							,@ExecPerson
							,@DownPersonName
							,GETDATE()
							,GETDATE()
							,1 --完成
							,GETDATE()
							,@CompanyId
							,2  --个人
						)

			
					--改变项目完成状态(完成日期不会因为产生新任务而发生变化)
					Update Project Set IsFinish=1 Where ProjectId=@ProjectId

				FETCH NEXT FROM My_Cursor INTO @ProjectId,@CusName,@ProductId,@ProductName,@ExecDepartmentId,@ExecPerson,@CompanyId; --读取第一行数据
			END
			CLOSE My_Cursor; --关闭游标
			DEALLOCATE My_Cursor; --释放游标
	   
	   		 --改变订单状态
			Update [Order] Set StateId=4 Where OId=@OrderId And StateId<4
			----------------------------------------------------------------------------------------------------------------------------
     End
	if @@ROWCOUNT<1
		begin
			set @errorSum=@errorSum+1		
			goto e;
		end

 e:
	if @errorSum>0
	Begin
		rollback TRANSACTION
	End
	Else
	Begin
		commit TRANSACTION
	End

	RETURN